using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace ScopaSL.Gameplay
{
    /// <summary>
    /// Gestiamo la logica del giocatore, ascoltando l'evento di fine turno e
    /// i clicks su una carta
    /// </summary>
    public abstract class PlayerLayer : GamePlayLayer
    {

        public PlayerLayer(BoardState state)
            : base(state)
        {
            //UpdateOrder = (int)ComponentOrders.LOGIC;
        }

        //public override void Initialize()
        //{
        //    Game.GetScopaBoardRenderer().ReleasePlayerCard += new ReleasePlayerCard(BoardManager_ReleasePlayerCard);

        //    base.Initialize();
        //}

        protected override void BoardManager_ReleasePlayerCard(int player_index, Card c, List<Card> selectedCards)
        {
            Combination validCombination = ComputeValidCombination(c, selectedCards);
            if (validCombination.cardsIndices.Any())
            {
                List<Card> combinationCards = new List<Card>();
                // Converto la lista di indici in una lista di carte
                for (int i = 0; i < validCombination.cardsIndices.Count; i++)
                    combinationCards.Add(blocks.ElementAt(0).Cards[validCombination.cardsIndices.ElementAt(i)]);

                playCardOnTable(c, combinationCards);
            }
            else
            {
                blocks.ElementAt(0).Cards.Add(c);
                players[player_index].Cards.Remove(c);

                TurnOver();
            }
        }
    }
}
